package pe.gob.mpfn.model;

import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Table(name = "personal")
@Configurable
@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "personal")
@RooDbManaged(automaticallyDelete = true)
public class Personal {

	@OneToMany(mappedBy = "intPersonalId")
    private Set<DesarrolloProteccion> desarrolloProteccions;

	@OneToMany(mappedBy = "intResponsableId")
    private Set<Informe> informes;

	@ManyToOne
    @JoinColumn(name = "int_cargo_id", referencedColumnName = "int_tabla_id")
    private TablaMaestra intCargoId;

	@Column(name = "str_nombre", columnDefinition = "VARCHAR", length = 100)
    private String strNombre;

	@Column(name = "str_apellido_paterno", columnDefinition = "VARCHAR", length = 50)
    private String strApellidoPaterno;

	@Column(name = "str_apellido_materno", columnDefinition = "VARCHAR", length = 50)
    private String strApellidoMaterno;

	@Column(name = "str_usuario", columnDefinition = "VARCHAR", length = 45)
    private String strUsuario;

	@Column(name = "str_password", columnDefinition = "VARCHAR", length = 45)
    private String strPassword;

	@Column(name = "chr_estado", columnDefinition = "CHAR")
    private Character chrEstado;

	public Set<DesarrolloProteccion> getDesarrolloProteccions() {
        return desarrolloProteccions;
    }

	public void setDesarrolloProteccions(Set<DesarrolloProteccion> desarrolloProteccions) {
        this.desarrolloProteccions = desarrolloProteccions;
    }

	public Set<Informe> getInformes() {
        return informes;
    }

	public void setInformes(Set<Informe> informes) {
        this.informes = informes;
    }

	public TablaMaestra getIntCargoId() {
        return intCargoId;
    }

	public void setIntCargoId(TablaMaestra intCargoId) {
        this.intCargoId = intCargoId;
    }

	public String getStrNombre() {
        return strNombre;
    }

	public void setStrNombre(String strNombre) {
        this.strNombre = strNombre;
    }

	public String getStrApellidoPaterno() {
        return strApellidoPaterno;
    }

	public void setStrApellidoPaterno(String strApellidoPaterno) {
        this.strApellidoPaterno = strApellidoPaterno;
    }

	public String getStrApellidoMaterno() {
        return strApellidoMaterno;
    }

	public void setStrApellidoMaterno(String strApellidoMaterno) {
        this.strApellidoMaterno = strApellidoMaterno;
    }

	public String getStrUsuario() {
        return strUsuario;
    }

	public void setStrUsuario(String strUsuario) {
        this.strUsuario = strUsuario;
    }

	public String getStrPassword() {
        return strPassword;
    }

	public void setStrPassword(String strPassword) {
        this.strPassword = strPassword;
    }

	public Character getChrEstado() {
        return chrEstado;
    }

	public void setChrEstado(Character chrEstado) {
        this.chrEstado = chrEstado;
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "int_personal_id", columnDefinition = "INT")
    private Integer intPersonalId;

	public Integer getIntPersonalId() {
        return this.intPersonalId;
    }

	public void setIntPersonalId(Integer id) {
        this.intPersonalId = id;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Personal().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countPersonals() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Personal o", Long.class).getSingleResult();
    }

	public static List<Personal> findAllPersonals() {
        return entityManager().createQuery("SELECT o FROM Personal o", Personal.class).getResultList();
    }

	public static Personal findPersonal(Integer intPersonalId) {
        if (intPersonalId == null) return null;
        return entityManager().find(Personal.class, intPersonalId);
    }

	public static List<Personal> findPersonalEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Personal o", Personal.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Personal attached = Personal.findPersonal(this.intPersonalId);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Personal merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Personal merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
}
